<!doctype html>



  


<html class="theme-next muse use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="logback," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.1" />






<meta name="description" content="logback 常用配置详解标签（空格分隔）： logback

根节点包含的属性
scan: 当此属性设置为true时，配置文件如果发生改变，将会被重新加载，默认值为true。
scanPeriod: 设置监测配置文件是否有修改的时间间隔，如果没有给出时间单位，默认单位是毫秒。当scan为true时，此属性生效。默认的时间间隔为1分钟。
debug: 当此属性设置为true时，将打印出logba">
<meta property="og:type" content="article">
<meta property="og:title" content="logback-configuration">
<meta property="og:url" content="http://yoursite.com/2016/09/21/logback-configuration/index.html">
<meta property="og:site_name" content="个人博客">
<meta property="og:description" content="logback 常用配置详解标签（空格分隔）： logback

根节点包含的属性
scan: 当此属性设置为true时，配置文件如果发生改变，将会被重新加载，默认值为true。
scanPeriod: 设置监测配置文件是否有修改的时间间隔，如果没有给出时间单位，默认单位是毫秒。当scan为true时，此属性生效。默认的时间间隔为1分钟。
debug: 当此属性设置为true时，将打印出logba">
<meta property="og:image" content="http://logback.qos.ch/manual/images/chapters/configuration/basicSyntax.png">
<meta property="og:updated_time" content="2017-01-19T07:14:13.404Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="logback-configuration">
<meta name="twitter:description" content="logback 常用配置详解标签（空格分隔）： logback

根节点包含的属性
scan: 当此属性设置为true时，配置文件如果发生改变，将会被重新加载，默认值为true。
scanPeriod: 设置监测配置文件是否有修改的时间间隔，如果没有给出时间单位，默认单位是毫秒。当scan为true时，此属性生效。默认的时间间隔为1分钟。
debug: 当此属性设置为true时，将打印出logba">
<meta name="twitter:image" content="http://logback.qos.ch/manual/images/chapters/configuration/basicSyntax.png">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 0,
      author: '博主'
    }
  };
</script>




  <link rel="canonical" href="http://yoursite.com/2016/09/21/logback-configuration/"/>

  <title> logback-configuration | 个人博客 </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">个人博客</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">随便写点什么</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                logback-configuration
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2016-09-21T11:50:22+08:00" content="2016-09-21">
              2016-09-21
            </time>
          </span>

          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/2016/09/21/logback-configuration/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2016/09/21/logback-configuration/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="logback-常用配置详解"><a href="#logback-常用配置详解" class="headerlink" title="logback 常用配置详解"></a>logback 常用配置详解</h1><p>标签（空格分隔）： logback</p>
<hr>
<h2 id="根节点包含的属性"><a href="#根节点包含的属性" class="headerlink" title="根节点包含的属性"></a>根节点<configuration>包含的属性</configuration></h2><ul>
<li><strong>scan:</strong> 当此属性设置为true时，配置文件如果发生改变，将会被重新加载，默认值为true。</li>
<li><strong>scanPeriod:</strong> 设置监测配置文件是否有修改的时间间隔，如果没有给出时间单位，默认单位是毫秒。当scan为true时，此属性生效。默认的时间间隔为1分钟。</li>
<li><strong>debug:</strong> 当此属性设置为true时，将打印出logback内部日志信息，实时查看logback运行状态。默认值为false。</li>
</ul>
<a id="more"></a>
<p>例如:<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">configuration</span> <span class="attr">scan</span>=<span class="string">"true"</span> <span class="attr">scanPeriod</span>=<span class="string">"60 seconds"</span> <span class="attr">debug</span>=<span class="string">"false"</span>&gt;</span>  </div><div class="line">      <span class="comment">&lt;!-- 其他配置省略--&gt;</span>  </div><div class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h2 id="根节点的子节点"><a href="#根节点的子节点" class="headerlink" title="根节点的子节点"></a>根节点<configuration>的子节点</configuration></h2><p><img src="http://logback.qos.ch/manual/images/chapters/configuration/basicSyntax.png" alt="img"></p>
<h3 id="设置上下文名称-contextName"><a href="#设置上下文名称-contextName" class="headerlink" title="设置上下文名称:contextName"></a>设置上下文名称:contextName</h3><p>每个logger都关联到logger上下文，默认上下文名称为default。但可以使用<strong>contextName</strong>设置成其他名字，用于区分不同应用程序的记录。一旦设置，不能修改。<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- lang: xml--&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">configuration</span> <span class="attr">scan</span>=<span class="string">"true"</span> <span class="attr">scanPeriod</span>=<span class="string">"60 seconds"</span> <span class="attr">debug</span>=<span class="string">"false"</span>&gt;</span>  </div><div class="line">      <span class="tag">&lt;<span class="name">contextName</span>&gt;</span>myAppName<span class="tag">&lt;/<span class="name">contextName</span>&gt;</span>  </div><div class="line">      <span class="comment">&lt;!-- 其他配置省略--&gt;</span>  </div><div class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h3 id="设置变量：property"><a href="#设置变量：property" class="headerlink" title="设置变量：property"></a>设置变量：property</h3><p>用来定义变量值的标签，<property> 有两个属性name和value；</property></p>
<ul>
<li>name: 变量的名称</li>
<li>value: 的值时变量定义的值。</li>
</ul>
<p>通过<strong>property</strong>定义的值会被插入到logger上下文中。定义变量后，可以使“${}”来使用变量。</p>
<p>例如使用<strong>property</strong>定义上下文名称，然后在<strong>contentName</strong>设置logger上下文时使用</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- lang: xml--&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">configuration</span> <span class="attr">scan</span>=<span class="string">"true"</span> <span class="attr">scanPeriod</span>=<span class="string">"60 seconds"</span> <span class="attr">debug</span>=<span class="string">"false"</span>&gt;</span>  </div><div class="line">      <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"APP_Name"</span> <span class="attr">value</span>=<span class="string">"myAppName"</span> /&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">contextName</span>&gt;</span>$&#123;APP_Name&#125;<span class="tag">&lt;/<span class="name">contextName</span>&gt;</span>  </div><div class="line">      <span class="comment">&lt;!-- 其他配置省略--&gt;</span>  </div><div class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div></pre></td></tr></table></figure>
<h3 id="获取时间戳字符串：timestamp"><a href="#获取时间戳字符串：timestamp" class="headerlink" title="获取时间戳字符串：timestamp"></a>获取时间戳字符串：timestamp</h3><p>两个属性:</p>
<ul>
<li>key: 标识此<timestamp> 的名字；</timestamp></li>
<li>datePattern: 设置将当前时间（解析配置文件的时间）转换为字符串的模式，遵循java.txt.SimpleDateFormat的格式。</li>
</ul>
<p>例如将解析配置文件的时间作为上下文名称：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">!-- lang: xml--&gt;</div><div class="line"><span class="tag">&lt;<span class="name">configuration</span> <span class="attr">scan</span>=<span class="string">"true"</span> <span class="attr">scanPeriod</span>=<span class="string">"60 seconds"</span> <span class="attr">debug</span>=<span class="string">"false"</span>&gt;</span>  </div><div class="line">      <span class="tag">&lt;<span class="name">timestamp</span> <span class="attr">key</span>=<span class="string">"bySecond"</span> <span class="attr">datePattern</span>=<span class="string">"yyyyMMdd'T'HHmmss"</span>/&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">contextName</span>&gt;</span>$&#123;bySecond&#125;<span class="tag">&lt;/<span class="name">contextName</span>&gt;</span>  </div><div class="line">      <span class="comment">&lt;!-- 其他配置省略--&gt;</span>  </div><div class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h3 id="设置loger"><a href="#设置loger" class="headerlink" title="设置loger"></a>设置loger</h3><p>用来设置某一个包或者具体的某一个类的日志打印级别、以及指定<strong>appender</strong>。<strong>loger</strong>仅有一个name属性，一个可选的level和一个可选的addtivity属性。</p>
<ul>
<li><p>name: 用来指定受此loger约束的某一个包或者具体的某一个类。</p>
</li>
<li><p>level: 用来设置打印级别，大小写无关：TRACE, DEBUG, INFO,WARN,ERROR,ALL和OFF，还有一个特俗值INHERITED或者同义词NULL`，代表强制执行上级的级别。 如果未设置此属性，那么当前loger将会继承上级的级别。</p>
</li>
<li><p>addtivity: 是否向上级loger传递打印信息。默认是true。</p>
</li>
</ul>
<p><strong>root</strong><br>也是<strong>loger</strong>元素，但是它是根loger。只有一个level属性，应为已经被命名为”root”.</p>
<ul>
<li>level: 用来设置打印级别，大小写无关：TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF，不能设置为INHERITED或者同义词NULL。 默认是DEBUG。</li>
</ul>
<p><strong>loger</strong>和<strong>root</strong>可以包含零个或多个<strong>appender-ref</strong>元素，标识这个appender将会添加到这个loger。</p>
<p>例如： LogbackDemo.java类<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">&lt;!-- lang: java--&gt;</div><div class="line"><span class="keyword">package</span> logback;  </div><div class="line"></div><div class="line"><span class="keyword">import</span> org.slf4j.Logger;  </div><div class="line"><span class="keyword">import</span> org.slf4j.LoggerFactory;  </div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LogbackDemo</span> </span>&#123;  </div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> Logger log = LoggerFactory.getLogger(LogbackDemo.class);  </div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;  </div><div class="line">        log.trace(<span class="string">"======trace"</span>);  </div><div class="line">        log.debug(<span class="string">"======debug"</span>);  </div><div class="line">        log.info(<span class="string">"======info"</span>);  </div><div class="line">        log.warn(<span class="string">"======warn"</span>);  </div><div class="line">        log.error(<span class="string">"======error"</span>);  </div><div class="line">    &#125;  </div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h2 id="logback-xml配置文件"><a href="#logback-xml配置文件" class="headerlink" title="logback.xml配置文件"></a>logback.xml配置文件</h2><h3 id="第1种：只配置root"><a href="#第1种：只配置root" class="headerlink" title="第1种：只配置root"></a>第1种：只配置root</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- lang: xml--&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span>   </div><div class="line"></div><div class="line">  <span class="tag">&lt;<span class="name">appender</span> <span class="attr">name</span>=<span class="string">"STDOUT"</span> <span class="attr">class</span>=<span class="string">"ch.qos.logback.core.ConsoleAppender"</span>&gt;</span>   </div><div class="line">    <span class="comment">&lt;!-- encoder 默认配置为PatternLayoutEncoder --&gt;</span>   </div><div class="line">    <span class="tag">&lt;<span class="name">encoder</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">pattern</span>&gt;</span>%d&#123;HH:mm:ss.SSS&#125; [%thread] %-5level %logger&#123;36&#125; - %msg%n<span class="tag">&lt;/<span class="name">pattern</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;/<span class="name">encoder</span>&gt;</span>   </div><div class="line">  <span class="tag">&lt;/<span class="name">appender</span>&gt;</span>   </div><div class="line"></div><div class="line">  <span class="tag">&lt;<span class="name">root</span> <span class="attr">level</span>=<span class="string">"INFO"</span>&gt;</span>             </div><div class="line">    <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">"STDOUT"</span> /&gt;</span>   </div><div class="line">  <span class="tag">&lt;/<span class="name">root</span>&gt;</span>     </div><div class="line"></div><div class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div></pre></td></tr></table></figure>
<p>其中appender的配置表示打印到控制台(稍后详细讲解appender )； &lt; root level=”INFO” &gt; 将root的打印级别设置为“INFO”，指定了名字为“STDOUT”的appender。</p>
<p>当执行logback.LogbackDemo类的main方法时，root将级别为“INFO”及大于“INFO”的日志信息交给已经配置好的名为“STDOUT”的appender处理，“STDOUT”appender将信息打印到控制台；</p>
<p>打印结果如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">13:30:38.484 [main] INFO  logback.LogbackDemo - ======info  </div><div class="line">13:30:38.500 [main] WARN  logback.LogbackDemo - ======warn  </div><div class="line">13:30:38.500 [main] ERROR logback.LogbackDemo - ======error</div></pre></td></tr></table></figure></p>
<h3 id="第2种：带有loger的配置，不指定级别，不指定appender："><a href="#第2种：带有loger的配置，不指定级别，不指定appender：" class="headerlink" title="第2种：带有loger的配置，不指定级别，不指定appender："></a>第2种：带有loger的配置，不指定级别，不指定appender：</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- lang: xml--&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span>   </div><div class="line"></div><div class="line">  <span class="tag">&lt;<span class="name">appender</span> <span class="attr">name</span>=<span class="string">"STDOUT"</span> <span class="attr">class</span>=<span class="string">"ch.qos.logback.core.ConsoleAppender"</span>&gt;</span>   </div><div class="line">    <span class="comment">&lt;!-- encoder 默认配置为PatternLayoutEncoder --&gt;</span>   </div><div class="line">    <span class="tag">&lt;<span class="name">encoder</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">pattern</span>&gt;</span>%d&#123;HH:mm:ss.SSS&#125; [%thread] %-5level %logger&#123;36&#125; - %msg%n<span class="tag">&lt;/<span class="name">pattern</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;/<span class="name">encoder</span>&gt;</span>   </div><div class="line">  <span class="tag">&lt;/<span class="name">appender</span>&gt;</span>   </div><div class="line"></div><div class="line">  <span class="comment">&lt;!-- logback为java中的包 --&gt;</span>   </div><div class="line">  <span class="tag">&lt;<span class="name">logger</span> <span class="attr">name</span>=<span class="string">"logback"</span>/&gt;</span>   </div><div class="line"></div><div class="line">  <span class="tag">&lt;<span class="name">root</span> <span class="attr">level</span>=<span class="string">"DEBUG"</span>&gt;</span>             </div><div class="line">    <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">"STDOUT"</span> /&gt;</span>   </div><div class="line">  <span class="tag">&lt;/<span class="name">root</span>&gt;</span>     </div><div class="line"></div><div class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div></pre></td></tr></table></figure>
<p>其中appender的配置表示打印到控制台(稍后详细讲解appender )； &lt;logger name=”logback” /&gt;将控制logback包下的所有类的日志的打印，但是并没用设置打印级别，所以继承他的上级<root>的日志级别“DEBUG”； 没有设置addtivity，默认为true，将此loger的打印信息向上级传递； 没有设置appender，此loger本身不打印任何信息。 &lt;root level=”DEBUG” &gt;将root的打印级别设置为“DEBUG”，指定了名字为“STDOUT”的appender。</root></p>
<p>当执行logback.LogbackDemo类的main方法时，因为LogbackDemo 在包logback中，所以首先执行&lt;logger name=”logback” /&gt;，将级别为“DEBUG”及大于“DEBUG”的日志信息传递给root，本身并不打印； root接到下级传递的信息，交给已经配置好的名为“STDOUT”的appender处理，“STDOUT”appender将信息打印到控制台；</p>
<p>打印结果如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">13:19:15.406 [main] DEBUG logback.LogbackDemo - ======debug</div><div class="line">13:19:15.406 [main] INFO  logback.LogbackDemo - ======info  </div><div class="line">13:19:15.406 [main] WARN  logback.LogbackDemo - ======warn  </div><div class="line">13:19:15.406 [main] ERROR logback.LogbackDemo - ======error</div></pre></td></tr></table></figure></p>
<h3 id="带有多个loger的配置，指定级别，指定appender"><a href="#带有多个loger的配置，指定级别，指定appender" class="headerlink" title="带有多个loger的配置，指定级别，指定appender:"></a>带有多个loger的配置，指定级别，指定appender:</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- lang: xml--&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span>   </div><div class="line">   <span class="tag">&lt;<span class="name">appender</span> <span class="attr">name</span>=<span class="string">"STDOUT"</span> <span class="attr">class</span>=<span class="string">"ch.qos.logback.core.ConsoleAppender"</span>&gt;</span>   </div><div class="line">    <span class="comment">&lt;!-- encoder 默认配置为PatternLayoutEncoder --&gt;</span>   </div><div class="line">    <span class="tag">&lt;<span class="name">encoder</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">pattern</span>&gt;</span>%d&#123;HH:mm:ss.SSS&#125; [%thread] %-5level %logger&#123;36&#125; - %msg%n<span class="tag">&lt;/<span class="name">pattern</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;/<span class="name">encoder</span>&gt;</span>   </div><div class="line">  <span class="tag">&lt;/<span class="name">appender</span>&gt;</span>   </div><div class="line"></div><div class="line">  <span class="comment">&lt;!-- logback为java中的包 --&gt;</span>   </div><div class="line">  <span class="tag">&lt;<span class="name">logger</span> <span class="attr">name</span>=<span class="string">"logback"</span>/&gt;</span>   </div><div class="line">  <span class="comment">&lt;!--logback.LogbackDemo：类的全路径 --&gt;</span>   </div><div class="line">  <span class="tag">&lt;<span class="name">logger</span> <span class="attr">name</span>=<span class="string">"logback.LogbackDemo"</span> <span class="attr">level</span>=<span class="string">"INFO"</span> <span class="attr">additivity</span>=<span class="string">"false"</span>&gt;</span>  </div><div class="line">    <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">"STDOUT"</span>/&gt;</span>  </div><div class="line">  <span class="tag">&lt;/<span class="name">logger</span>&gt;</span>   </div><div class="line"></div><div class="line">  <span class="tag">&lt;<span class="name">root</span> <span class="attr">level</span>=<span class="string">"ERROR"</span>&gt;</span>             </div><div class="line">    <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">"STDOUT"</span> /&gt;</span>   </div><div class="line">  <span class="tag">&lt;/<span class="name">root</span>&gt;</span>     </div><div class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div></pre></td></tr></table></figure>
<p>其中appender的配置表示打印到控制台(稍后详细讲解appender )；</p>
<p>&lt;logger name=”logback” /&gt;将控制logback包下的所有类的日志的打印，但是并没用设置打印级别，所以继承他的上级&lt;root&gt;的日志级别“DEBUG”； 没有设置addtivity，默认为true，将此loger的打印信息向上级传递； 没有设置appender，此loger本身不打印任何信息。</p>
<p>&lt;logger name=”logback.LogbackDemo” level=”INFO” additivity=”false”&gt;控制logback.LogbackDemo类的日志打印，打印级别为“INFO”； additivity属性为false，表示此loger的打印信息不再向上级传递， 指定了名字为“STDOUT”的appender。</p>
<p>&lt;root level=”DEBUG”&gt;将root的打印级别设置为“ERROR”，指定了名字为“STDOUT”的appender。</p>
<p>当执行logback.LogbackDemo类的main方法时，先执行&lt;logger name=”logback.LogbackDemo” level=”INFO” additivity=”false”&gt;，将级别为“INFO”及大于“INFO”的日志信息交给此loger指定的名为“STDOUT”的appender处理，在控制台中打出日志，不再向次loger的上级 &lt;logger name=”logback”/&gt; 传递打印信息； &lt;logger name=”logback”/&gt;未接到任何打印信息，当然也不会给它的上级root传递任何打印信息；</p>
<p>打印结果如下:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">14:09:01.531 [main] INFO  logback.LogbackDemo - ======info  </div><div class="line">14:09:01.531 [main] INFO  logback.LogbackDemo - ======info  </div><div class="line">14:09:01.531 [main] WARN  logback.LogbackDemo - ======warn  </div><div class="line">14:09:01.531 [main] WARN  logback.LogbackDemo - ======warn  </div><div class="line">14:09:01.531 [main] ERROR logback.LogbackDemo - ======error </div><div class="line">14:09:01.531 [main] ERROR logback.LogbackDemo - ======error</div></pre></td></tr></table></figure></p>
<p>原文地址 <a href="http://blog.csdn.net/haidage/article/details/6794509" target="_blank" rel="external">http://blog.csdn.net/haidage/article/details/6794509</a></p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/logback/" rel="tag">#logback</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/09/21/redis-cluster/" rel="next" title="redis3.x 集群搭建">
                <i class="fa fa-chevron-left"></i> redis3.x 集群搭建
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/11/22/spring-boot的一些总结/" rel="prev" title="spring boot的一些总结">
                spring boot的一些总结 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2016/09/21/logback-configuration/"
           data-title="logback-configuration" data-url="http://yoursite.com/2016/09/21/logback-configuration/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/uploads/touxiang.jpg"
               alt="钱伟峰" />
          <p class="site-author-name" itemprop="name">钱伟峰</p>
          <p class="site-description motion-element" itemprop="description">每天进步一点点</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">21</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">7</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">11</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#logback-常用配置详解"><span class="nav-number">1.</span> <span class="nav-text">logback 常用配置详解</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#根节点包含的属性"><span class="nav-number">1.1.</span> <span class="nav-text">根节点包含的属性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#根节点的子节点"><span class="nav-number">1.2.</span> <span class="nav-text">根节点的子节点</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#设置上下文名称-contextName"><span class="nav-number">1.2.1.</span> <span class="nav-text">设置上下文名称:contextName</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#设置变量：property"><span class="nav-number">1.2.2.</span> <span class="nav-text">设置变量：property</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#获取时间戳字符串：timestamp"><span class="nav-number">1.2.3.</span> <span class="nav-text">获取时间戳字符串：timestamp</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#设置loger"><span class="nav-number">1.2.4.</span> <span class="nav-text">设置loger</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#logback-xml配置文件"><span class="nav-number">1.3.</span> <span class="nav-text">logback.xml配置文件</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#第1种：只配置root"><span class="nav-number">1.3.1.</span> <span class="nav-text">第1种：只配置root</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#第2种：带有loger的配置，不指定级别，不指定appender："><span class="nav-number">1.3.2.</span> <span class="nav-text">第2种：带有loger的配置，不指定级别，不指定appender：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#带有多个loger的配置，指定级别，指定appender"><span class="nav-number">1.3.3.</span> <span class="nav-text">带有多个loger的配置，指定级别，指定appender:</span></a></li></ol></li></ol></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">钱伟峰</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>

        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.1"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"qianweifeng"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    <script src="/vendors/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  






  
  

  

  

  

</body>
</html>
